<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file provinceandcity.php
 * @brief 
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-08-02
 */



class Controller_Provinceandcity extends Controller_Rest {
	public function before()
	{
		parent::before();
	}

	/**
	 * @brief 获取指定起止位置的类型
	 *
	 * @return $this 
	 */
	protected function get_objc_list()
	{
		/** 获取起止数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);
		$order = filter_input(INPUT_GET, 'order');

		$term = ORM::factory('provinceandcity');
	
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos : PHP_INT_MAX;

		$order = ($order and ($order == 'asc' or $order == 'desc')) ? $order : 'asc';
		
		$term->order_by(DB::expr("CONVERT(name USING GBK )"),$order);
		$terms = $term
				->where('term_id', '>', 20)
				->and_where('term_id', '<', 55)
				->offset($start)
				->limit($pos)
				->find_all();
		if(!$terms->count())
		{
			$ret['flag'] = 0;
			$ret['num'] = 0;
			$ret['description'] = 'provinceandcity not found!';
			$json_ret = json_encode($ret);
			$this->response->body($json_ret)->status(206);
		}
		else
		{
			/** 返回条数及列表*/
			$res['flag'] = 1;			
			$res['order'] = $order;
			$res['num'] = $terms->count();
			$res['list'] = array();
			foreach($terms as $tem)
			{
				$res['list'][] = $tem->as_array(PROVINCECITY_CUSGET);
			}
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
	}
}
